/**
 * 路线制定控制类
 * @author zqd	差旅小组
 *
 * 上午11:21:56
 */
package com.cbts.action.travel;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.web.context.ServletContextAware;

import com.cbts.entity.BusinessTravel;
import com.cbts.entity.BusinessTravelList;
import com.cbts.entity.TravelStandard;
import com.cbts.entity.UserInfo;
import com.cbts.service.info.TravelInfoService;
import com.cbts.service.info.UserInfoService;
import com.cbts.service.info.VerifierService;
import com.cbts.service.info.impl.UserInfoServiceImpl;
import com.cbts.service.travel.RoutesetService;
import com.opensymphony.xwork2.ActionSupport;

public class RouteSetAction extends ActionSupport {
	
	private RoutesetService routesetService;
	private TravelInfoService travelInfoService;
	private UserInfoService userInfoService;
	private VerifierService verifierService;	//差旅审核业务逻辑接口

	
	private String mapMessage;					//路线信息
	private String jsonResult;
	
	private List<BusinessTravel> routeList;
	private BusinessTravel route;
	private List<BusinessTravelList> btlist;
	private String travelOrder;
	
	private String starttime;
	private String endtime;
	private String alterstarttime;
	private String alterendtime;
	
	private TravelStandard userts;
	
	private int isingroup;

	public void setRoutesetService(RoutesetService routesetService) {
		this.routesetService = routesetService;
	}
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	public void setTravelInfoService(TravelInfoService travelInfoService) {
		this.travelInfoService = travelInfoService;
	}

	
	public VerifierService getVerifierService() {
		return verifierService;
	}
	public void setVerifierService(VerifierService verifierService) {
		this.verifierService = verifierService;
	}
	public int getIsingroup() {
		return isingroup;
	}
	public void setIsingroup(int isingroup) {
		this.isingroup = isingroup;
	}
	public List<BusinessTravel> getRouteList() {
		return routeList;
	}
	public void setRouteList(List<BusinessTravel> routeList) {
		this.routeList = routeList;
	}
	public BusinessTravel getRoute() {
		return route;
	}
	public void setRoute(BusinessTravel route) {
		this.route = route;
	}
	public List<BusinessTravelList> getBtlist() {
		return btlist;
	}
	public void setBtlist(List<BusinessTravelList> btlist) {
		this.btlist = btlist;
	}
	public String getTravelOrder() {
		return travelOrder;
	}
	public void setTravelOrder(String travelOrder) {
		this.travelOrder = travelOrder;
	}
	public TravelStandard getUserts() {
		return userts;
	}
	public void setUserts(TravelStandard userts) {
		this.userts = userts;
	}
	public String getStarttime() {
		return starttime;
	}
	public void setStarttime(String starttime) {
		this.starttime = starttime;
	}
	public String getEndtime() {
		return endtime;
	}
	public void setEndtime(String endtime) {
		this.endtime = endtime;
	}
	public String getAlterstarttime() {
		return alterstarttime;
	}
	public void setAlterstarttime(String alterstarttime) {
		this.alterstarttime = alterstarttime;
	}
	public String getAlterendtime() {
		return alterendtime;
	}
	public void setAlterendtime(String alterendtime) {
		this.alterendtime = alterendtime;
	}
	
	public String getMapMessage() {
		return mapMessage;
	}
	public void setMapMessage(String mapMessage) {
		this.mapMessage = mapMessage;
	}
	public String getJsonResult() {
		return jsonResult;
	}
	public void setJsonResult(String jsonResult) {
		this.jsonResult = jsonResult;
	}
	
	/**
	 * 获得地图信息
	 * @return
	 */
	public String getMapResult(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String routeStr=request.getParameter("routeStr");		//获得前段传来的起终坐标,以及查询
		System.out.println(routeStr);
		String url = "http://www.tianditu.com/query.shtml";
		
		//通过转发，将数据传递给天地图的引擎，并获取返回结果
		String postString="type=search&postStr="+routeStr;		//传递的数据
		mapMessage=routesetService.accessUrl(url,postString);	//获得的xml信息
		
		System.out.println(mapMessage);

		return "maproute";
	}
	
	/**
	 * 路线制定前的数据准备
	 * @return
	 */
	public String routeSetPrepareSetting(){
		//获得该员工的标准与其他相关数据
		//传入到路线制定页面
		//return "prepaersetting"	return ERROR
		Map session=ServletActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		this.travelOrder=request.getParameter("travelOrder");
		
		this.routeList=null;							//一开始要设为null，不然会影响后续的修改
		this.alterstarttime=null;
		this.alterendtime=null;
		
		String userid=(String)session.get("userid");	//获得session中的userid
		UserInfo user=userInfoService.getUserInfo(userid);
		
		if(user.getUserGroup()==null||user.getUserGroup().getTravelStandard()==null){	//不在一个用户组中或者该组无差旅标准
			this.isingroup=0;
		}else{
			this.isingroup=1;
			userts=travelInfoService.getTravelStandardInfoById(user.getUserGroup().getTravelStandard().getStandardId());
			
			if(travelOrder!=null){
				this.routeList=routesetService.getBusinessTravelInfo(travelOrder, user.getUserid()).getPageItems();
				BusinessTravelList btl=routesetService.getBusinessTravelListByOrder(travelOrder, user.getUserid());
				this.alterstarttime=new SimpleDateFormat("yyyy-MM-dd").format(btl.getStartTime());
				this.alterendtime=new SimpleDateFormat("yyyy-MM-dd").format(btl.getEndTime());
			}
		}
		return "prepaersetting";
	}
	
	/**
	 * 路线设定，修改路线与其同样
	 * @return
	 */
	public String setRoute(){
		//获得从前台传来的list
		//获取始末时间
		//调用routesetService的submitRoute，提交路线
		//return "setroute"	return "setfail"
		Date startdate = java.sql.Date.valueOf(starttime);
		Date enddate = java.sql.Date.valueOf(endtime);
		Map session=ServletActionContext.getContext().getSession();
		String userid=(String)session.get("userid");
		if(routesetService.submitRoute(routeList, userid, userInfoService.getUserInfo(userid).getUserGroup().
				getTravelStandard().getStandardId(), startdate, enddate)){	//提交成功
			{
				List<BusinessTravelList> b = verifierService.getBusinessTravelLists("待审核");
				if(b!=null){
					session.put("needCheck", b.size());
				}
				return "setroute";
			}
			
		}else{
			return "setfail";
		}
	}
	
	/**
	 * 获得路线列表
	 * @return
	 */
	public String showRouteList(){
		//通过session获得员工的id
		//通过id查找该员工的所有制定路线
		//return "showroutelist"	return "showfail"
		try{
			Map session=ServletActionContext.getContext().getSession();
			String userid=(String)session.get("userid");
			this.setBtlist(routesetService.getPendingBusinessTravelList(userid).getPageItems());
			return "showroutelist";
		}catch(Exception e){
			e.printStackTrace();
			return "showfail";
		}
	}
	
	/**
	 * 设定完成之后
	 */
	public String afterSet(){
		try{
			Map session=ServletActionContext.getContext().getSession();
			String userid=(String)session.get("userid");
			this.setBtlist(routesetService.getPendingBusinessTravelList(userid).getPageItems());
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("routesetsuccess", "1");
			return "showroutelist";
		}catch(Exception e){
			e.printStackTrace();
			return "showfail";
		}
	}
	
	
	/**
	 * 获得单个BusinessTravel并返回
	 * @return
	 */
	public String showSingleRoute(){
		////通过前台传入的参数判断所引用的travelorder
		//调用RoutesetService的getBusinessTravelInfo获得该单独路线的list
		//返回到前台
		//return "showsingleroute"	return "showsinglefail"
		HttpServletRequest request = ServletActionContext.getRequest();
		String to=request.getParameter("travelOrder");
		try{
			Map session=ServletActionContext.getContext().getSession();
			String userid=(String)session.get("userid");
			List<BusinessTravel> bslist=routesetService.getBusinessTravelInfo(to, userid).getPageItems();
			JSONArray jsondata=new JSONArray();		//转换工具
			JsonConfig config=new JsonConfig();
			config.setExcludes(new String[]{"userInfo","businessTravelList","travelStandard"});
			jsondata=JSONArray.fromObject(bslist,config);
			jsonResult=jsondata.toString();
			
			//System.out.println(jsonResult);
			
			return "showsingleroute";
		}catch(Exception e){
			e.printStackTrace();
			return "showsinglefail";
		}
	}
	
	/**
	 * 修改路线
	 * @return
	 */
	public String alterRoute(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int sign=Integer.parseInt(request.getParameter("sign"));		//这个sign是为了当list的个数少于修改前的list的长度时，删掉多余的条目
		String to=(String)request.getParameter("travelOrder");
		Date startdate = java.sql.Date.valueOf(alterstarttime);
		Date enddate = java.sql.Date.valueOf(alterendtime);
		
		//删除多余的条目
		int length=routeList.size();
		if(sign<length){						//当前长度小于原长度
			for(int i=length-1;i>sign-1;i--){	//length-sign--应该删除的条目数
				routeList.remove(i);
			}
		}
		//

		
		//获得session中的userid
		Map session=ServletActionContext.getContext().getSession();
		String userid=(String)session.get("userid");
		UserInfo user=userInfoService.getUserInfo(userid);
		
		int ts=user.getUserGroup().getTravelStandard().getStandardId();
		//
		if(routesetService.alterRoute(routeList, to, user.getUserid(), ts, startdate, enddate)){	//提交成功
			{
				List<BusinessTravelList> b = verifierService.getBusinessTravelLists("待审核");
				if(b!=null){
					session.put("needCheck", b.size());
				}
				return "alterroute";
			}
			
		}else{
			return "alterfail";
		}
	}


}
